package com.zhouys.relection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//通过反射动态创建对象
public class Test8 {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //获取class
        Class c1 = Class.forName("com.zhouys.relection.User");
//        //构造class对象
//        User user =(User) c1.newInstance();
//        System.out.println(user);

        //通过构造器创建对象
//        Constructor declaredConstructors = c1.getDeclaredConstructor(String.class,int.class,int.class);
//        User user2 = (User) declaredConstructors.newInstance("zhouys", 001, 25);
//        System.out.println(user2);


        //通过反射调用方法
        User user3 = (User)c1.newInstance();
        Method setName = c1.getDeclaredMethod("setName", String.class);
        setName.invoke(user3, "六六六");
        System.out.println(user3.getName());
        //

        //

        //
    }
}
